<template>
	<view>
		<u-avatar :src="src" @tap="showImg"></u-avatar>
	</view>
</template>

<script>
	import { getImageCache } from "./image.js"
	export default {
		name:"avatar",
		props: {
			url:{
				type: String,
				default: require('@/static/img2/face-default.png')
			},
			fileMd5:{
				type: String,
				default: ''
			}
		},
		data() {
			return {
				src:''
			};
		},
		watch: {
			// 监听头像md5值的变化
			fileMd5(val) { 
				// 查找获取图片缓存
				this.getImg()
			}
		},
		created() {
			// 查找获取图片缓存
			this.getImg()
		},
		methods: {
			showImg(){
				this.$emit("showImg", this.src)
			},
			// 查找获取图片缓存
			async getImg() { 
				// #ifdef APP-PLUS
				var result = await getImageCache(this.url, this.fileMd5)
				if (result) {
					this.src = result
				} else {
					this.src = this.url
				}
				// #endif
				// #ifndef APP-PLUS
				this.src = this.url
				// #endif
			}
		}
	}
</script>

<style lang="scss">
</style>